package com.gdut.simple.model.impl;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.ILoginModel;
import com.gdut.simple.model.IRegisterModel;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterModelImpl implements IRegisterModel {

    @Override
    public void register(final String email, final String password, final RequestCallback<User> callback) {
        final User user = new User();
        final ILoginModel loginModel = new LoginModelImpl();
        user.setUsername(email);
        user.setPassword(password);
        user.setNickName(email);
        user.setGender("男");
        user.signUp(new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null) {
                    loginModel.login(email, password, callback);
                } else {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                }
            }
        });
//        user.signUpObservable(User.class).subscribe(new Subscriber<User>() {
//            @Override
//            public void onCompleted() {
//                LogUtils.d("---signUp complete---");
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//                LogUtils.e(new BmobException(throwable).getMessage());
//                if (callback != null) {
//                    callback.onFailure(new BmobException(throwable).getMessage());
//                }
//            }
//
//            @Override
//            public void onNext(User bmobUser) {
//                LogUtils.d("onNext");
//                if (callback != null) {
//                    callback.onSuccess(bmobUser);
//                }
//            }
//        });
    }
}
